package handler

import (
	"fmt"
	"choujiang/box_server/service"
	"choujiang/common/consts"
	"choujiang/common/models"
	"choujiang/common/utils"

	"github.com/gin-gonic/gin"
)

func SignUp(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	fmt.Printf("user uniqueId is %s userId is %s", uniqueId, user.Id)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	ret := make(map[string]interface{})
	errType := service.DailySignUp(user)
	if errType != nil {
		utils.Error(errType, c)
		return
	}
	point, nextPoint := service.GetSignUpInfo(user)
	ret["point"] = point
	ret["nextPoint"] = nextPoint

	utils.JSON(ret, c)
}

func SignUpList(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	signUpList, err := service.GetSignUpList(user)
	if err != nil {
		fmt.Printf("SignUpList err:%+v\n", err)
	}

	//fmt.Printf("SignUpList:%+v\n", signUpList)

	utils.JSON(signUpList, c)
}

func GetTaskDetail(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	task := service.GetTaskInfo(user)

	utils.JSON(task, c)
}

func GetFootprint(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	footPrint := service.GetFootprint(user)
	if footPrint == nil {
		utils.Error(consts.ErrNoFootprint, c)
		return
	}
	utils.JSON(footPrint, c)
}

func ReceiveRecommend(c *gin.Context) {
	form := c.Request.Form
	gameId := form.Get("receiveGame")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	go service.ReceiveRecommend(user, gameId)
	utils.JSON("ok", c)
}
